package com.benyi.util;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature; 
 
public class JsonTool {
	
	public static String toStr(Object v) {
		if(v==null) {
			return null;
		}
		StringBuilder sb=new StringBuilder();
		if(v.getClass().isArray()) {
			Object []c=(Object[])v;
			sb.append("Array length="+c.length);
			for(int i=0;i<c.length;i++) {
				sb.append("\r\n");
				if(c[i]==null) {
					sb.append("index="+i+":null");
				}else {
					sb.append("index="+i+":"+c[i].toString());
				} 
			} 
		}else {
			if(  v instanceof List) {
				List c=(List)v;
				sb.append("List length="+c.size());
				for(int i=0;i<c.size();i++) {
					sb.append("\r\n");
					if(c.get(i)==null) {
						sb.append("index="+i+":null");
					}else {
						sb.append("index="+i+":"+c.get(i).toString());
					} 
				} 
			}else {
				return toString(v);
			}
		}
		return sb.toString();
	}
	
	public static String toString(Object v){
		if(v==null) {
			return null;
		}
		JSON.DEFFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
		String s=JSON.toJSONString(v, SerializerFeature.WriteDateUseDateFormat ,
				SerializerFeature.PrettyFormat);
		//String s=JSON.toJSONString(v);
		return s;
	}
	 
	/* new TypeReference<List<User>>() {} */
	
	public static <T> T parse(String json,Class<T> clazz){		
		T v=JSON.parseObject(json,clazz);
		return v;
	}
	
	public static <T> T parse(String json,TypeReference<T> type){		
		T v=JSON.parseObject(json,type);
		return v;
	}
	 
}
